<?php

/**
 * This is the model class for table "user_admin".
 *
 * The followings are the available columns in table 'user_admin':
 * @property integer $id
 * @property string $user_name
 * @property string $password
 * @property string $realname
 * @property integer $user_id
 * @property string $avatar
 * @property string $create_time
 * @property string $status
 */
class UserAdmin extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_admin';
	}
	
	public function getavatar() {
		$avatar = ($this->avatar) ? $this->avatar : Yii::app()->params['defaultAvatar'];
		$avatar = Yii::app()->params['imagePath'].$avatar;
		return $avatar;
		
	}
	
	public function scopes() {
		return array(
			'active' => array('condition' => 'status = :status','params'=>[':status'=>'正常'])
		);
	}
	
	
	

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('user_name', 'unique'),
			array('user_name,realname', 'required'),
			array('password','required','on'=>'create'),
			array('user_name', 'length', 'max'=>20),
			array('password', 'length', 'max'=>32),
			array('realname', 'length', 'max'=>20),
			array('create_time', 'default','value'=>date('Y-m-d H:i:s')),
			array('user_name, realname, user_id, status', 'safe', 'on'=>'search'),
		);
	}

	protected function beforeSave() {
		//密码加密
		if($this->password)
		{
			$this->password = $this->hashPassword($this->password);
		}
		return parent::beforeSave();
	}
	
	public function hashPassword($password) {
		return crypt($password,'uuvio.com');
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_name' => '用户名',
			'password' => '密码',
			'realname' => '真实姓名',
			'user_id' => '友声用户ID',
			'avatar' => '头像',
			'create_time' => '创建日期',
			'status' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('realname',$this->realname,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return UserAdmin the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
